package net.oschina.app.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;

import net.oschina.app.R;
import net.oschina.app.adapter.MeBlogRvAdapter;
import net.oschina.app.base.BaseAdapter;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.bean.BlogBean;
import net.oschina.app.bean.MeBlogBean;
import net.oschina.app.protocol.MeBlogProtocol;
import net.oschina.app.ui.DetailActivity;
import net.oschina.app.util.CommonUtils;

import java.util.List;

/**
 * Created by elf on 2017/1/15.
 */

public class MeBlogFragment extends BaseFragment {
    private MeBlogRvAdapter mMeBlogRvAdapter;
    private List<BlogBean> mBlogBean;
    @Override
    protected BaseAdapter getRvAdapter() {
        mMeBlogRvAdapter = new MeBlogRvAdapter();
        return mMeBlogRvAdapter;
    }

    @Override
    protected String getFragmentTitle() {
        return getString(R.string.actionbar_title_blog);
    }

    @Override
    protected int onLoadSubData() {
        MeBlogProtocol meBlogProtocol = new MeBlogProtocol();
        final MeBlogBean meBlogBean = meBlogProtocol.getData(mList.size());
        if (meBlogBean != null) {
            mList.addAll(meBlogBean.getBlogs());
            CommonUtils.runOnMainThread(new Runnable() {
                @Override
                public void run() {
                    mBlogBean = meBlogBean.getBlogs();
                    mMeBlogRvAdapter.setDatas(mBlogBean);
                }
            });
            return STATE_SUCCESS;
        }
        return STATE_EMPTY;
    }

    @Override
    protected void OnSubItemClick(RecyclerAdapterWithHF adapter, RecyclerView.ViewHolder vh, int position) {
        super.OnSubItemClick(adapter, vh, position);
        Intent intent = new Intent(getContext(), DetailActivity.class);
        BlogBean blogBean = mBlogBean.get(position);
        intent.putExtra("url", blogBean.getUrl());
        intent.putExtra("title", getFragmentTitle());
        startActivity(intent);
    }
}
